package com.simon.mctm.biz.sys.service.impl;

import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.toolkit.CollectionUtils;
import com.simon.mctm.base.PageResult;
import com.simon.mctm.biz.sys.bean.Dict;
import com.simon.mctm.biz.sys.dao.DictMapper;
import com.simon.mctm.biz.sys.service.DictService;
import com.baomidou.mybatisplus.service.impl.ServiceImpl;
import com.simon.mctm.exception.OperationFailedException;
import com.simon.mctm.utils.Utils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * <p>
 * 业务字典 服务实现类
 * </p>
 *
 * @author simon
 * @since 2018-06-15
 */
@Service
public class DictServiceImpl extends ServiceImpl<DictMapper, Dict> implements DictService {

    @Autowired
    private DictMapper dictMapper;

    /**
     * 根据条件获取总条目数和总页数
     *
     * @param code     编码
     * @param name     名称
     * @param pageSize 单页条目数
     * @return 页数和总条目数对象
     */
    @Override
    public PageResult calPage(String code, String name, int pageSize) {
        try {
            List<Dict> dictList = dictMapper.selectList(
                    new EntityWrapper<Dict>().like("code", code)
                            .like("name", name)
            );
            if (CollectionUtils.isNotEmpty(dictList)) {
                return new PageResult(dictList.size(), Utils.calPages(dictList.size(), pageSize));
            } else {
                return new PageResult(0, 0);
            }
        } catch (Exception ex) {
            throw new OperationFailedException(ex.getMessage());
        }
    }
}
